const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
      StatusBar: app.globalData.StatusBar,
      CustomBar: app.globalData.CustomBar,
      Custom: app.globalData.Custom,
      list: [],
      tabCur: 0,
      mainCur: 0,
      verticalNavTop: 0,
      load: true,
      haveImg: app.globalData.haveImg,
      bgimg1: app.globalData.bgimg,

  },
  
  methods: {
    TabSelect(e){
      console.log(e)
      this.setData({
        tabCur: e.currentTarget.dataset.id,
        MainCur: e.currentTarget.dataset.id,
        VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
      })
    },
    goTodetail: function (e) {
      console.log(e)
      //传参
      let info = JSON.stringify(e.currentTarget.dataset)
      console.log(info)
      //单独页面单独传参
      wx.navigateTo({
        url: '/pages/home/detail/detail?info=' + info,
      })
    },
    VerticalMain(e) {
      let that = this;
      let list = this.data.list;
      let tabHeight = 0;
      if (this.data.load) {
        for (let i = 0; i < list.length; i++) {
          let view = wx.createSelectorQuery().select("#main-" + list[i].id);
          view.fields({
            size: true
          }, data => {
            list[i].top = tabHeight;
            tabHeight = tabHeight + data.height;
            list[i].bottom = tabHeight;
          }).exec();
        }
        that.setData({
          load: false,
          list: list
        })
      }
      let scrollTop = e.detail.scrollTop + 20;
      for (let i = 0; i < list.length; i++) {
        if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
          that.setData({
            VerticalNavTop: (list[i].id - 1) * 50,
            TabCur: list[i].id
          })
          return false
        }
      }
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
      this.setData({
        ListTouchStart: e.touches[0].pageX
      })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.setData({
        ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
      })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.data.ListTouchDirection == 'left') {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
      } else {
        this.setData({
          modalName: null
        })
      }
      this.setData({
        ListTouchDirection: null
      })
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      var that =this
        //得到roomlist从而得到用户数目
        wx.request({
          // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
          method: 'POST',
          data: {
            token: app.globalData.userInfo.token,
            userId: app.globalData.userInfo.userInfo.id
          },
          success: function (res) {
            console.log(res)
            app.globalData.RoomCount = res.data.list
            that.setData({
              message: res.data.list,
            })
          }
        })
    //得到userlist 从而得到number
    wx.request({
          // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
          method: 'POST',
          data: {
            token: app.globalData.userInfo.token,
            userId: app.globalData.userInfo.userInfo.id
          },
          success: function (res) {
            console.log(res)
            //除非有改动否则不用重新刷新
            app.globalData.FriendCount = res.data.list
            that.setData({
              list: res.data.list,
              bgimg1: app.globalData.bgimg,
              haveImg: app.globalData.haveImg
            })
          }
        })
        
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  }
  

});